package com.qf;

import com.qf.dao.UserDao;
import com.qf.entity.User;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Before;
import org.junit.Test;
import java.io.InputStream;
import java.util.Date;
import java.util.List;


public class Demo {

    SqlSessionFactory sqlSessionFactory = null;
    @Before
    public void before() throws Exception{
        //编写文件
        String resource = "mybatis-config.xml";
        //读取配置文件
        InputStream inputStream = Resources.getResourceAsStream(resource);
        //构建sqlSession工厂
        sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
    }

    @Test
    public void test01(){
        try(SqlSession session = sqlSessionFactory.openSession(true)){
            UserDao mapper = session.getMapper(UserDao.class);
            User user = new User(0,"韩梅梅","123456","女",new Date(),new Date());
            int insert = mapper.insert(user);
            System.out.println(insert);
        }
    }

    @Test
    public void test02(){
        try(SqlSession session = sqlSessionFactory.openSession(true)){
            UserDao mapper = session.getMapper(UserDao.class);
            int insert = mapper.delete(10);
            System.out.println(insert);
        }
    }

    @Test
    public void test03(){
        try(SqlSession session = sqlSessionFactory.openSession(true)){
            UserDao mapper = session.getMapper(UserDao.class);
            User user = new User(8,"阿迪达斯","123456","男",new Date(),new Date());
            int insert = mapper.update(user);
            System.out.println(insert);
        }
    }

    @Test
    public void test04(){
        try(SqlSession session = sqlSessionFactory.openSession(true)){
            UserDao mapper = session.getMapper(UserDao.class);
            List<User> users = mapper.select();
            for (User user:users) {
                System.out.println(user);
            }
        }
    }


}
